package com.tianhui.consignortwofei.other.auth.recognition;

import android.content.Context;

import com.baidu.ocr.sdk.OCR;
import com.baidu.ocr.sdk.OnResultListener;
import com.baidu.ocr.sdk.exception.OCRError;
import com.baidu.ocr.sdk.model.IDCardResult;
import com.baidu.ocr.sdk.model.OcrRequestParams;
import com.baidu.ocr.sdk.model.OcrResponseResult;
import com.baidu.ocr.sdk.utils.Parser;
import com.fgs.common.utils.LogUtil;
import com.fgs.common.widget.CustomProgressDialog;
import com.tianhui.consignortwofei.mvp.model.enty.RecognitionInfo;
import com.tianhui.consignortwofei.mvp.model.enty.SelectPhotoInfo;
import com.tianhui.consignortwofei.mvp.model.enty.authLicense.VehicleLicenseData;
import com.tianhui.consignortwofei.mvp.model.enty.ocrResult.vehicleLicense.VehicleLicenseResult;
import com.tianhui.consignortwofei.mvp.model.enty.ocrResult.vehicleLicense.VehicleLicenseResultParser;
import com.tianhui.consignortwofei.other.auth.inte.RecognitionCallback;
import com.tianhui.consignortwofei.utils.ApplicationUtil;

import java.io.File;

/**
 * @author fgs
 * @date 2021/5/13
 * @description
 **/
public class VehicleBackRecognition implements IRecognition<VehicleLicenseData> {


    @Override
    public void recognition(Context context, SelectPhotoInfo selectPhotoInfo, RecognitionCallback<VehicleLicenseData> callback) {
        OcrRequestParams param = new OcrRequestParams();
        param.putParam("vehicle_license_side", "back");
        param.setImageFile(new File(selectPhotoInfo.filePath));

        CustomProgressDialog dialog = new CustomProgressDialog(context);
        dialog.show();

        OCR.getInstance(ApplicationUtil.getContext()).recognizeVehicleLicense(param, new OnResultListener<OcrResponseResult>() {
            @Override
            public void onResult(OcrResponseResult result) {
                dialog.dismiss();
                if (result != null) {
                    LogUtil.print(result.getJsonRes());
                    VehicleLicenseResult vehicleLicenseResult = null;
                    try {
                        Parser<VehicleLicenseResult> resultParser = new VehicleLicenseResultParser();
                        vehicleLicenseResult = resultParser.parse(result.getJsonRes());
                        LogUtil.print(vehicleLicenseResult.toString());
                    } catch (OCRError ocrError) {
                        ocrError.printStackTrace();
                    }

                    if (vehicleLicenseResult != null) {
                        RecognitionInfo recognitionInfo = new RecognitionInfo<IDCardResult>();
                        recognitionInfo.info = vehicleLicenseResult;
                        recognitionInfo.filePath = selectPhotoInfo.filePath;
                        if (null != callback)
                            callback.recognitionSuccess(recognitionInfo);
                    }
                }
            }

            @Override
            public void onError(OCRError error) {
                LogUtil.print(error.getMessage());
                dialog.dismiss();
                if (null != callback)
                    callback.recognitionFailed();
            }
        });
    }
}
